package com.gxa.insurance.service.impl;


import com.gxa.insurance.exception.BusinessException;
import com.gxa.insurance.exception.ExceptionCode;
import com.gxa.insurance.mapper.DriverCommentsMapper;
import com.gxa.insurance.mapper.OrderMapper;
import com.gxa.insurance.pojo.DriverComments;
import com.gxa.insurance.pojo.Order;
import com.gxa.insurance.service.DriverCommentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author shen gang
 * @date 2022/6/7
 */
@Service
@Transactional(rollbackFor = {Throwable.class})
public class DriverCommentsServiceImpl implements DriverCommentsService {
    @Autowired
    DriverCommentsMapper driverCommentsMapper;

    @Autowired
    OrderMapper orderMapper;


    @Override
    public List<DriverComments> selectAll() {
        List<DriverComments> driverComments = driverCommentsMapper.selectAll();
        return driverComments;
    }

    @Override
    public List<DriverComments> selectLikeName(String name, String orderNumber) throws BusinessException {

        Order order=new Order();
            order.setName(name);
            order.setDiver(name);
            order.setNumber(name);

        List<DriverComments> driverComments1 = driverCommentsMapper.selectLikeName(order);

        if (driverComments1.size() ==0){
            throw  new BusinessException(ExceptionCode.EMPTY_LIST);
        }

        return driverComments1;
    }

    @Override
    public Map<String, Object> selectByPage(Integer page, Integer size) throws Exception {
        Map<String,Object> map = new HashMap();
        Integer count = driverCommentsMapper.selectAllCount();
        List<DriverComments> driverComments = driverCommentsMapper.selectByPage((page - 1) * size, size);
        System.out.println(driverComments);
        if(driverComments.size() == 0){
            throw new BusinessException(ExceptionCode.CUSTOMER_IS_EMPTY);
        }
        map.put("count",count);
        map.put("customers",driverComments);
        return map;
    }

    @Override
    public Integer delete(Integer id) throws BusinessException {

        Integer delete = driverCommentsMapper.delete(id);
        if (delete==0){
            throw new BusinessException(ExceptionCode.DELETE_FILED);
        }
        return delete;
    }

//    @Override
//    public Integer add(DriverComments driverComments) {
//
//        return null;
//    }

    @Override
    public DriverComments selectComments(Integer id) throws BusinessException {

        DriverComments driverComments = driverCommentsMapper.selectComments(id);
        if (driverComments==null){
            throw new BusinessException(ExceptionCode.EMPTY_LIST);
        }


        return driverComments;
    }


    @Override
    public  List<DriverComments> selectByTime(Map map) throws Exception {

        Map<String, Object> maps = new HashMap<>();


        // 转换传入的时间字符串对象
        Object createTime = map.get("createTime");
        Object overTime = map.get("overTime");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
        if (createTime!=null &&!"".equals(createTime)&&!"null".equals(createTime)) {
            Date create = simpleDateFormat.parse(String.valueOf(createTime));
            map.put("createTime",create);
        }
        if (overTime!= null &&!"".equals(overTime)&&!"null".equals(overTime)) {
            Date over = simpleDateFormat.parse(String.valueOf(overTime));
            map.put("overTime",over);
        }
        // 处理page 和 pageSize
        Object page = map.get("page");
        Object pageSize = map.get("pageSize");
        // 类型转换
        int intPage = Integer.parseInt(String.valueOf(page));
        int intPageSize = Integer.parseInt(String.valueOf(pageSize));
        if (intPage <= 0) {
            intPage = 1;
        } else if (intPageSize <= 0) {
            intPageSize = 10;
        }
        map.put("page", (intPage - 1) * intPageSize);
        map.put("pageSize", intPageSize);
        // 调用 mapper 查询
        List<DriverComments> driverComments =driverCommentsMapper.selectByTime(map);
//        Integer count = mapper.selectByfLikeCount(map);
//        int size = driverComments.size();
        maps.put("list", driverComments);
//        maps.put("size", size);
        return  driverComments;
    }

    @Override
    public List<DriverComments> getCommentsList(String name) {

        List<DriverComments> driverComments = new ArrayList<>();
        List<String> orders = orderMapper.selectNumberByDriver(name);
        orders.forEach(System.out::println);
        for (int i = 0; i < orders.size(); i++) {
            String number = orders.get(i);
            System.out.println(number);
            DriverComments getDriverComments = driverCommentsMapper.selectCommentsByNumber(number);
            driverComments.add(getDriverComments);
        }
        return driverComments;
    }


}
